//package com.qianya.weishi_server.websocket;
//
//
//import com.qianya.weishi_server.entity.PhpAdminInfo;
//import com.qianya.weishi_server.entity.PhpUserInfo;
//import com.qianya.weishi_server.service.HttpToPhp;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.http.server.ServerHttpRequest;
//import org.springframework.http.server.ServerHttpResponse;
//import org.springframework.http.server.ServletServerHttpRequest;
//import org.springframework.http.server.ServletServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.socket.WebSocketHandler;
//import org.springframework.web.socket.server.HandshakeInterceptor;
//
//import javax.annotation.Resource;
//import java.util.Map;
//
//@Slf4j
//@Component
//public class WebSocketInterceptor implements HandshakeInterceptor {
//
//    @Resource
//    private HttpToPhp httpToPhp;
//
//    /**
//     * 握手之前
//     *
//     * @param request    request
//     * @param response   response
//     * @param wsHandler  handler
//     * @param attributes 属性
//     * @return 是否握手成功：true-成功，false-失败
//     */
//    @Override
//    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
//                                   Map<String, Object> attributes) {
//        ServletServerHttpRequest serverHttpRequest = (ServletServerHttpRequest) request;
//        ServletServerHttpResponse serverHttpResponse = (ServletServerHttpResponse) response;
//        String authorization = serverHttpRequest.getServletRequest().getHeader("Sec-WebSocket-Protocol");
//        if (!StringUtils.isEmpty(authorization)) {
//            serverHttpResponse.getServletResponse().setHeader("Sec-WebSocket-Protocol", authorization);
//        } else {
//            authorization = serverHttpRequest.getServletRequest().getHeader("Sec-WebSocket-Protocol".toLowerCase());
//            serverHttpResponse.getServletResponse().setHeader("Sec-WebSocket-Protocol".toLowerCase(), authorization);
//        }
////        authorization = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" +
////                ".eyJyb2xlIjoiYWRtaW4iLCJ1dWlkIjoiMjMwNjA1Mz" +
////                "QzNjYxMTE1OTYzNzU1OTEyMzc2ODI4NjEiLCJ0aW1lIjoxNj" +
////                "kwMjY4NjQ3LCJvdGhlciI6eyJuYW1lIjoi5p2o5Li75Lu7IiwidXVpZCI6IjIzMDYwNTM0Mz" +
////                "Y2MTExNTk2Mzc1NTkxMjM3NjgyODYxIiwibmlja25hbWUiOiLlkozolLznmoTmnajkuLvku7siLCJwcm9maWxlIjoiIn0sImF1Z" +
////                "CI6IiIsImV4cCI6MTY5MDM1NTA0NywiaWF0IjoxNjkwMjY4NjQ3LCJpc3MiOiIiLCJqdGkiOiJiMjdjNGY5ZDI3OGJiMjBhYWQ3MzMyZWQyYzg3NTkw" +
////                "YiIsIm5iZiI6MTY5MDI2ODY0Nywic3ViIjoiIn0.mesITsdWqATgMGVWamIY-OJ77Z87UwuzebUmLA26crA";
//        if (StringUtils.isBlank(authorization)) {
//            log.error("websocket未携带token");
//            return false;
//        }
//        PhpUserInfo phpUserInfoCunsumer;
//        PhpAdminInfo phpAdminInfoAdmin = httpToPhp.getPhpUserInfoAdmin(authorization);
//        if (phpAdminInfoAdmin == null) {
//            phpUserInfoCunsumer = httpToPhp.getPhpUserInfoCunsumer(authorization);
//            if (phpUserInfoCunsumer == null){
//                log.error("未查询到用户信息");
//                return false;
//            }
//            attributes.put("uuid", phpUserInfoCunsumer.getUuid());
//            attributes.put("name", phpUserInfoCunsumer.getName());
//            log.info("uid:【" + phpUserInfoCunsumer.getUuid() + "】，用户:【" + phpUserInfoCunsumer.getName() + "】成功握手");
//            return true;
//        }else{
//            attributes.put("uuid", phpAdminInfoAdmin.getUuid());
//            attributes.put("name", phpAdminInfoAdmin.getName());
//            log.info("uid:【" + phpAdminInfoAdmin.getUuid() + "】，用户:【" + phpAdminInfoAdmin.getName() + "】成功握手");
//            return true;
//        }
//
//    }
//    /**
//     * 握手后
//     *
//     * @param request   request
//     * @param response  response
//     * @param wsHandler wsHandler
//     * @param exception exception
//     */
//    @Override
//    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
//                               Exception exception) {
//        log.info("握手成功");
//    }
//}
